<template>
  <div class="tea-timeline">
    <div class="timeline-header">
      <h2  style="background: linear-gradient(45deg, #f8e8ba, #d09d54); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">茶饮作用时间轴</h2>
      <div class="tea-type-selector">
        <span 
          v-for="(teaType, index) in Object.keys(teaEffects)" 
          :key="index"
          :class="{ active: currentTeaType === teaType }"
          @click="switchTeaType(teaType)"
        >
          {{ teaType }}
        </span>
      </div>
    </div>
    <div class="timeline-container">
      <div class="timeline-line"></div>
      <div class="timeline-items" :style="{ transform: `translateX(${timelineOffset}px)` }">
        <div v-for="(item, index) in currentEffects" :key="index" class="timeline-item">
          <div class="timeline-dot"></div>
          <div class="timeline-content">
            <div class="time">{{ item.time }}</div>
            <div class="icon">{{ item.icon }}</div>
            <div class="organ">{{ item.organ }}</div>
            <div class="effect">{{ item.effect }}</div>
          </div>
        </div>
      </div>
      <div class="scroll-controls">
        <button @click="scrollLeft" class="scroll-btn">←</button>
        <button @click="scrollRight" class="scroll-btn">→</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue';

const teaEffects = {
  "绿茶": [
    {"time": "0h", "organ": "口腔", "effect": "", "icon": "🧪"},
    {"time": "0.5h", "organ": "胃部", "effect": "", "icon": "⏱️"},
    {"time": "1h", "organ": "肝脏", "effect": "", "icon": "♻️"},
    {"time": "3h", "organ": "大脑", "effect": "", "icon": "🧠"},
    {"time": "6h", "organ": "肠道", "effect": "", "icon": "🦠"},
    {"time": "12h", "organ": "心血管", "effect": "", "icon": "❤️"},
    {"time": "24h", "organ": "全身", "effect": "", "icon": "⚖️"}
  ],
  "红茶": [
    {"time": "0.5h", "organ": "口腔", "effect": "", "icon": "🦷"},
    {"time": "1.5h", "organ": "心脏", "effect": "", "icon": "💓"},
    {"time": "4h", "organ": "肌肉", "effect": "", "icon": "💪"},
    {"time": "8h", "organ": "肾脏", "effect": "", "icon": "🚰"}
  ],
  "青茶": [
    {"time": "1h", "organ": "胃部", "effect": "", "icon": "🛡️"},
    {"time": "2h", "organ": "肝脏", "effect": "", "icon": "⚡"},
    {"time": "5h", "organ": "皮肤", "effect": "", "icon": "✨"}
  ],
  "白茶": [
    {"time": "2h", "organ": "免疫系统", "effect": "", "icon": "🛡️"},
    {"time": "6h", "organ": "肺部", "effect": "", "icon": "🫁"}
  ],
  "黑茶": [
    {"time": "3h", "organ": "肠道", "effect": "", "icon": "🦠"},
    {"time": "12h", "organ": "血管", "effect": "", "icon": "🩸"}
  ],
  "黄茶": [
    {"time": "1h", "organ": "胰腺", "effect": "", "icon": "🍬"},
    {"time": "4h", "organ": "大脑", "effect": "", "icon": "😌"}
  ]
};

const currentTeaType = ref('绿茶');
const timelineOffset = ref(0);
let autoScrollInterval = null;

const currentEffects = computed(() => teaEffects[currentTeaType.value]);

const scrollLeft = () => {
  timelineOffset.value = Math.min(0, timelineOffset.value + 300);
};

const scrollRight = () => {
  const containerWidth = document.querySelector('.timeline-container').offsetWidth;
  const itemsWidth = document.querySelector('.timeline-items').scrollWidth;
  const maxOffset = containerWidth - itemsWidth;
  timelineOffset.value = Math.max(maxOffset, timelineOffset.value - 300);
};

const switchTeaType = (teaType) => {
  currentTeaType.value = teaType;
  timelineOffset.value = 0;
};

const startAutoScroll = () => {
  autoScrollInterval = setInterval(() => {
    const teaTypes = Object.keys(teaEffects);
    const currentIndex = teaTypes.indexOf(currentTeaType.value);
    const nextIndex = (currentIndex + 1) % teaTypes.length;
    switchTeaType(teaTypes[nextIndex]);
  }, 5000);
};

onMounted(() => {
  startAutoScroll();
});

onUnmounted(() => {
  if (autoScrollInterval) {
    clearInterval(autoScrollInterval);
  }
});
</script>

<style scoped>
.tea-timeline {
  width: 100%;
  height: 100%;
  padding: 10px;
  background: rgba(0, 20, 50, 0.5);
  border-radius: 10px;
  color: #fff;
  position: relative;
  overflow: hidden;
  box-sizing: border-box;
}

.timeline-header {
  text-align: center;
  margin-bottom: 20px;
}

.timeline-header h2 {
  color: #32ccee;
  font-size: 22px;
  margin-bottom: 15px;
}

.tea-type-selector {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.tea-type-selector span {
  padding: 8px 20px;
  border-radius: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.1);
  display: inline-block;
  min-width: 60px;
  text-align: center;
  user-select: none;
}

.tea-type-selector span.active {
  background: linear-gradient(45deg, #f8e8ba, #d09d54);
  color: #fff;
  box-shadow: 0 0 10px rgba(50, 204, 238, 0.5);
}

.tea-type-selector span:hover {
  background: rgba(50, 204, 238, 0.2);
  transform: translateY(-2px);
}

.timeline-container {
  position: relative;
  height: 220px;
  overflow: hidden;
  margin-top: -40px;
  width: 100%;
  box-sizing: border-box;
}

.timeline-line {
  position: absolute;
  top: 40%;
  left: 0;
  right: 0;
  height: 3px;
  background: rgba(50, 204, 238, 0.5);
  transform: translateY(-50%);
}

.timeline-items {
  position: absolute;
  top: 40%;
  left: 0;
  display: flex;
  transition: transform 0.3s ease;
  transform: translateY(-50%);
  padding: 0 15px;
  width: 100%;
  box-sizing: border-box;
}

.timeline-item {
  position: relative;
  margin: 0 5px;
  display: flex;
  flex-direction: column;
  align-items: center;
  opacity: 0;
  transform: translateY(20px);
  animation: fadeInUp 0.5s ease forwards;
  flex-shrink: 0;
}

.timeline-dot {
  width: 10px;
  height: 10px;
  background: #32ccee;
  border-radius: 50%;
  position: relative;
  margin-bottom: 8px;
  box-shadow: 0 0 10px rgba(50, 204, 238, 0.7);
}

.timeline-content {
  width: 130px;
  padding: 15px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  text-align: center;
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.timeline-content:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
  background: rgba(255, 255, 255, 0.1);
}

.time {
  color: #32ccee;
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 6px;
}

.icon {
  font-size: 20px;
  margin-bottom: 6px;
}

.organ {
  font-weight: bold;
  margin-bottom: 4px;
  font-size: 14px;
}

.effect {
  font-size: 13px;
  color: #ddd;
  line-height: 1.3;
}

.scroll-controls {
  position: absolute;
  top: 40%;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  padding: 0 10px;
  transform: translateY(-50%);
  pointer-events: none;
  z-index: 2;
}

.scroll-btn {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: rgba(50, 204, 238, 0.4);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: all 0.3s ease;
  pointer-events: auto;
}

.scroll-btn:hover {
  background: rgba(50, 204, 238, 0.5);
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.timeline-item:nth-child(1) { animation-delay: 0.1s; }
.timeline-item:nth-child(2) { animation-delay: 0.2s; }
.timeline-item:nth-child(3) { animation-delay: 0.3s; }
.timeline-item:nth-child(4) { animation-delay: 0.4s; }
.timeline-item:nth-child(5) { animation-delay: 0.5s; }
.timeline-item:nth-child(6) { animation-delay: 0.6s; }
.timeline-item:nth-child(7) { animation-delay: 0.7s; }
</style>